package mbp.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import mbp.gui.frames.MbpManagerGui;
import mbp.message.contract;
import mbp.message.extra;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

public class BusinessAgentGuiExpl extends JPanel implements ActionListener
{
	private MbpManagerGui mbpManagerGui;
	private OfferListPanel offerListPanel;
	private ContractListPanel contractListPanel;
	private JTextField txtAgentName;
	
	private JButton btnAddAgent;
	private JButton btnDeleteAgent;
	private JComboBox cmbAgents;
	private String activeAgent = "";
	
	
	public BusinessAgentGuiExpl(MbpManagerGui mbpManagerGui)
	{
		this.mbpManagerGui = mbpManagerGui;
		setLayout(null);
		
		offerListPanel = new OfferListPanel() {
			public void processDoubleClick(int index)
			{
				//getDetailsRC(index);
			}
		};
		
		offerListPanel.setBorder(new TitledBorder(null, "Offers", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		offerListPanel.setBounds(151, 242, 600, 160);
		add(offerListPanel);
		
		contractListPanel = new ContractListPanel() {
			public void processDoubleClick(int index)
			{
				//getDetailsRC(index);
			}
		};
		
		contractListPanel.setBorder(new TitledBorder(null, "Contracts", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contractListPanel.setBounds(151, 11, 600, 229);
		add(contractListPanel);
		
		JPanel panelAgent = new JPanel();
		panelAgent.setBorder(new TitledBorder(null, "Agent", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelAgent.setBounds(10, 11, 135, 391);
		add(panelAgent);
		panelAgent.setLayout(null);
		
		txtAgentName = new JTextField();
		txtAgentName.setBounds(10, 36, 115, 23);
		panelAgent.add(txtAgentName);
		txtAgentName.setColumns(10);
		
		btnAddAgent = new JButton("Add");
		btnAddAgent.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnAddAgent.setBounds(10, 67, 115, 23);
		panelAgent.add(btnAddAgent);
		btnAddAgent.addActionListener(this);
		
		cmbAgents = new JComboBox();
		cmbAgents.setBounds(10, 135, 115, 22);
		panelAgent.add(cmbAgents);
		
		btnDeleteAgent = new JButton("Delete");
		btnDeleteAgent.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnDeleteAgent.setBounds(10, 168, 115, 23);
		panelAgent.add(btnDeleteAgent);
		btnDeleteAgent.setEnabled(false);
		btnDeleteAgent.addActionListener(this);
		
		JLabel lblNewLabel = new JLabel("Agent name");
		lblNewLabel.setFont(new Font("Dialog", Font.PLAIN, 11));
		lblNewLabel.setBounds(10, 21, 86, 14);
		panelAgent.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Agents to trace");
		lblNewLabel_1.setFont(new Font("Dialog", Font.PLAIN, 11));
		lblNewLabel_1.setBounds(10, 121, 115, 14);
		panelAgent.add(lblNewLabel_1);
		
		cmbAgents.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) 
			{
				
				int index = cmbAgents.getSelectedIndex();
				if(index==-1)
				{
					return;
				}
				
				List<contract> emptyList = new ArrayList<contract>();
				contractListPanel.reloadListInfo(emptyList);
				
				if(activeAgent.compareTo("")!=0)
				{
					sendNotifyRequest(activeAgent, "0");
				}
				
				activeAgent = (String)cmbAgents.getItemAt(index);
				
				sendNotifyRequest(activeAgent, "1");
				//System.out.println("key: " + key);
				//LoadResource(resourcesStore.get(key));
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnAddAgent)
		{
			String agentName = txtAgentName.getText();
			cmbAgents.addItem(agentName);
			return;
		}
		
		if(e.getSource() == btnDeleteAgent)
		{
			return;
		}
		
	}
	
	public void sendNotifyRequest(String agentName, String notify)
	{
		extra ex1 = new extra();
		ex1.name = "notify";
		ex1.value = notify;
		mbpManagerGui.myAgent.sendExtra(ex1, agentName);
	}
	
	public String getActiveAgent() {
		return this.activeAgent;
	}
	
	public void reloadContracts(List<contract> cntrList)
	{
		contractListPanel.reloadListInfo(cntrList);
	}
}
